package com.yyy.instagram.fragmnet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lhh.apst.library.AdvancedPagerSlidingTabStrip;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.yyy.instagram.APSTSViewPager;
import com.yyy.instagram.Adaper.NewsAdapter;
import com.yyy.instagram.Adaper.RecyclerViewAdapter;
import com.yyy.instagram.R;
import com.yyy.instagram.ThridFragment;
import com.yyy.instagram.common.PullRefreshHeader.MyPullRefreshFrameLayout;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by qqqqqqq on 2016/10/9.
 */

public class NewsMeFragment extends Fragment {

    RecyclerView recyclerView;

    public static NewsMeFragment instance(){
        NewsMeFragment fragment = new NewsMeFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_me, null);
        findViews(view);
        initView();
        return view;
    }

    private void findViews(View view){
        recyclerView = (RecyclerView) view.findViewById(R.id.news_me_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        NewsAdapter adapter = new NewsAdapter();
        recyclerView.setAdapter(adapter);

        final MyPullRefreshFrameLayout ptrFrame = (MyPullRefreshFrameLayout) view.findViewById(R.id.news_me_ptr_frame);
        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrFrame.refreshComplete();
                    }
                }, 1800);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                LinearLayoutManager lm= (LinearLayoutManager) recyclerView.getLayoutManager();
                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                Log.d("dsad","positon"+topRowVerticalPosition);
                if(lm.findViewByPosition(lm.findFirstVisibleItemPosition()).getTop()==0 && lm.findFirstVisibleItemPosition()==0) {
                    return true;
                }else{
                    return false;
                }
            }
        });
    }

    private void initView(){

    }
}
